<!--左侧导航菜单-->
<template>
  <!--default-active为高亮菜单的索引，通过store中的当前打开的标签值实时获取-->
  <el-menu
    :default-active="this.$store.state.menus.editableTabsValue"
    class="el-menu-vertical-demo"
    background-color="#545c64"
    text-color="#fff"
    active-text-color="#ffd04b">

    <router-link to="/index">
      <el-menu-item index="Index" @click="selectMenu({title:'首页',name:'Index'})">
        <template slot="title">
          <i class="el-icon-s-home"></i>
          <span slot="title">首页</span>
        </template>
      </el-menu-item>
    </router-link>

    <el-submenu :index="menu.name" v-for="menu in menuList" :key="menu.name">  <!--循环遍历左侧菜单-->
      <template slot="title">
        <i :class="menu.icon"></i>
        <span>{{menu.title}}</span>
      </template>

      <router-link :to="item.path" v-for="item in menu.children" :key="item.name">
        <el-menu-item :index="item.name" @click="selectMenu(item)">
          <template slot="title">
            <i :class="item.icon"></i>
            <span slot="title">{{item.title}}</span>
          </template>
        </el-menu-item>
      </router-link>
    </el-submenu>

  </el-menu>
</template>

<script>
  export default {
    name: "SideMenu",
    data(){
      return {

      }
    },
    computed:{
      menuList:{  //放在computed中，可实现对menuList的动态监听，解决store中menuList晚于组件加载导致组件中为空
        get(){
          return this.$store.state.menus.menuList
        }
      }
    },
    methods:{
      selectMenu(item){ //用于动态添加标签页
        this.$store.commit("addTab",item);
      }
    }
  }
</script>

<style scoped>
  .el-menu{
    height: 100%;
  }
</style>
